package edu.humc.controller;

import edu.humc.pojo.User;
import edu.humc.service.UserService;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RequestMapping(value = "/user")
@Controller("UserController")
public class UserController {
    @Resource(name = "UserServiceImpl")
    UserService userService;
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String IsLogin(User user , Model model, HttpServletRequest request){
        HttpSession session= request.getSession();
        if (userService.UserLogin(user)){
            session.setAttribute("username" ,user.getUsername());
            session.setAttribute("id",userService.SelectUserId(user));
            model.addAttribute("username",user.getUsername());
//            调用查询id的service方法，并传至index页面(已经通过session技术代替)
//            model.addAttribute("id",userService.SelectUserId(user));
            return "/index.jsp";
        }
        //        利用model对象向前台传递提示信息
        model.addAttribute("errorMessage","登录错误");
        return "/error.jsp";
    }
//    注销登录方法
    @RequestMapping(value = "/logout")
    public String Logout(User user , HttpServletRequest request){
        HttpSession session= request.getSession();
//        注销用户的session
        session.invalidate();
        return "/user/UserLogin.jsp";
    }
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public String IsRegister(User user,Model model){
        if (userService.UserRegister(user)){
            return "/user/UserLogin.jsp";
        }
        //        利用model对象向前台传递提示信息
        model.addAttribute("errorMessage","注册错误");
        return "/error.jsp";
    }
    @RequestMapping(value = "/updateUser",method = RequestMethod.POST)
    public String UpdatePwd(User user,String newpassword, Model model){
        String errorMessage;
        if (userService.UpdateUser(user,newpassword)){
            model.addAttribute("message","密码修改成功！");
            return "/success.jsp";
        }
//        利用model对象向前台传递提示信息
        model.addAttribute("errorMessage","密码修改错误");
        return "/error.jsp";
    }
    @RequestMapping(value = "/DeleteUser")
    public String DeleteUser(int id,Model model){
//        判断是否注销用户成功
        if (userService.DeleteUser(id)) {
            model.addAttribute("message","用户注销成功！");
            return "/success.jsp";
        }
        //        利用model对象向前台传递提示信息
        model.addAttribute("errorMessage","用户注销错误");
        return "/index.jsp";
    }
//    查找用户所有信息，实现个人中心功能
    @RequestMapping("/UserMessage")
    public String DisplayUserMessage(HttpSession session){
//        取出登录控制器中在session中存入的id值
        int id=(int)session.getAttribute("id");
        User user=userService.SelectUserByid(id);
        session.setAttribute("user",user);
        return "/UserMessage.jsp";
    }
//    更新用户信息
    @RequestMapping(value = "/UpdatePersonMessage",method = RequestMethod.POST)
    public String UpdatePersonMessage(User user,HttpSession session,Model model){
        int id=(int)session.getAttribute("id");
        user.setId(id);
        if (userService.UpdatePersonMessage(user)) {
//            更新后存在bug，跳转后原有未修改的值不显示
            return "/user/UserMessage";
        }
        model.addAttribute("OpeateErrorMessage","修改个人信息错误！");
        return "/OpeateErrorPage.jsp";
    }
}
